package cn.hy.preparelesson.module.pub;


import cn.hy.preparelesson.common.constant.ProjectConstant;
import cn.hy.preparelesson.common.enums.DataStatus;
import cn.hy.preparelesson.common.enums.FileUploadType;
import cn.hy.preparelesson.common.utils.DateUtils;
import cn.hy.preparelesson.common.utils.IdUtils;
import cn.hy.preparelesson.common.vo.FileUploadResultVo;
import cn.hy.preparelesson.common.vo.Result;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/busi/upload")
public class UploadController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 统一上传图片 通过code区分文件上传路径
     * @param files
     */
    @RequestMapping("/file")
    public Result uploadAppImg(@RequestParam("files") MultipartFile[] files, @RequestParam byte code) {
        FileUploadType imgUploadType = FileUploadType.getByCode(code);
        if (imgUploadType == null) {
            return Result.error("上传参数异常！", "");
        }

        if(files.length == 0) {
            return Result.error("请选择文件！", "");
        }
        if (files.length > 3) {
            return Result.error("很抱歉，最多只能上传3个附件！", "");
        }
//        List<MultipartFile> fileList = Arrays.asList(files);
        List<FileUploadResultVo> resultList  = new ArrayList<>();
        for (int i = 0; i < files.length; i++) {
            if (files[i] != null) {
                try (InputStream inputStream = files[i].getInputStream()) {
                    String originFileName = files[i].getOriginalFilename();
                    //存到服务器的绝对路径
                    //扩展名
                    String fileExt = originFileName.substring(originFileName.lastIndexOf("."));
                    //新文件名
                    String newFileName = IdUtils.newId() + fileExt;
                    //最终存储路径
                    String newFilePath = ProjectConstant.fileUploadLocation + imgUploadType.getTypePath() + DateUtils.getDateDirPath() + "/" + newFileName;
                    logger.debug("源文件名为：{}，文件存储路径为：{}", originFileName, newFilePath);
                    File toFile = new File(newFilePath);
                    FileUtils.copyToFile(inputStream, toFile);
                    String filePath =  ProjectConstant.hostName + newFilePath.replace(ProjectConstant.fileUploadLocation, ProjectConstant.fileVisitPrefix);
                    resultList.add(new FileUploadResultVo(filePath, DataStatus.NORMAL.getCode(), i, originFileName));
                } catch (Exception e) {
                    logger.error("上传文件失败：", e);
                    resultList.add(new FileUploadResultVo( DataStatus.DELETE.getCode(), i));
                }
            }
        }
        return Result.success(resultList);
    }
}
